package cn.jluzh.blog.mapper;

import cn.jluzh.blog.modules.user.entity.SysUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SysUserMapper extends BaseMapper<SysUser> {

    /**
     * 校验用户名是否重复
     * @param account 用户名
     * @return String
     * */
    @Select("select account from sys_user where account = #{account}")
    String validAccount(String account);

    @Select("select email from sys_user where email=#{email}")
    String validEmail(String email);

    @Select("select * from sys_user where account = #{account}")
    SysUser getUserByAccount(String account);

    /**
     * 修改用户密码
     * @param id 用户id
     * @param oldPass 旧密码
     * @param newPass 新密码
     * */
    @Update("update sys_user set password = #{newPass} where id = #{id} and password = #{oldPass} ")
    void updatePwd(@Param("id") Integer id, @Param("oldPass") String oldPass, @Param("newPass") String newPass);

    /**
     * 根据用户id找到对应的密码
     * @param id 用户id
     * @return String
     * */
    @Select("select password from sys_user where id = #{id}")
    String getPwdById(Integer id);

    @Select("select * from sys_user su where nickname like CONCAT('%',#{query},'%')")
    List<SysUser> getAllUserByName(String query);
}
